import QtQuick 2.0
import QtGraphicalEffects 1.0
import "../../CMDSender.js" as Controler

Item {
    id:root

    scale:0
    opacity: 0.4
    Component.onCompleted: {
        scale=1;
        opacity=1;
    }
    ListModel{
        id:commandModel
    }
    Timer{
        id:commandTimer
        property int currentIndex:0
        repeat: currentIndex<commandModel.count
        interval: 650
        onTriggered: {
            if(TcpSocket.tcpState==="connected")
                TcpSocket.sendData(commandModel.get(currentIndex).cmd);
            else
            {
                var cmd=commandModel.get(currentIndex).cmd;
                cmd=cmd.substring(0,cmd.length-2);
                Controler.send(cmd);
            }

//                Controler.send()
            currentIndex++;
        }
    }

    Behavior on scale{
        PropertyAnimation{
            properties: "scale"
            duration: 1000
            easing.type: Easing.OutQuint
        }
    }
    Behavior on opacity{
        PropertyAnimation{
            properties: "opacity"
            duration: 1000
            easing.type: Easing.OutQuint
        }
    }

    //hidden bar
    Image{
        id:bottombar
        anchors.bottom: parent.bottom
        width: root.width
        source:"qrc:/imgs/home/smartHomeIcons/home/bottom_bar.png"
        height:60*Device.dentisty

        //bnts
        Row{
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 15*Device.dentisty
            anchors.horizontalCenter: parent.horizontalCenter
            spacing:(root.width-40*Device.dentisty)/5

            Image{
                id:seettingbnt
                source:"qrc:/imgs/home/smartHomeIcons/home/bnt_setting.png"
                width: 40*Device.dentisty
                height: width
            }

            Image{
                id:messagebnt
                source:"qrc:/imgs/home/smartHomeIcons/home/bnt_message.png"
                width: 40*Device.dentisty
                height: width
            }

            Image{
                id:userbnt
                source:"qrc:/imgs/home/smartHomeIcons/home/bnt_user.png"
                width: 40*Device.dentisty
                height: width
            }

            Image{
                id:ringgerbnt
                source:"qrc:/imgs/home/smartHomeIcons/home/bnt_ringger.png"
                width: 40*Device.dentisty
                height: width
            }
        }
    }

    //content
    RectangularGlow {
        id: effect
        anchors.fill: prePage
        glowRadius: 10*Device.dentisty
        spread: 0.2
        color: "#66000000"
        cornerRadius: prePage.radius + glowRadius
    }

    Rectangle{
        id: prePage
        width: parent.width
        height:parent.height
        Behavior on y{
            PropertyAnimation{
                properties:"y"
                duration: 200
            }
        }

        Image {
            id: bg
            source: "qrc:/imgs/home/smartHomeIcons/home/bg.png"
            anchors.fill: parent
        }

        Image {
            id: state_round_pad
            source: "qrc:/imgs/home/smartHomeIcons/home/state_pad.png"
            width: parent.width*2/3
            height:width
            anchors.horizontalCenter: parent.horizontalCenter
            y:100*Device.dentisty
            Column{
                anchors.centerIn: parent
                spacing: 15*Device.dentisty
                Text{
                    id:temText
                    y:20*Device.dentisty
                    text:"温度： 22℃"
                    font.pointSize: 20
                    color:"#afafaf"
                }

                Text{
                    id:lighthText
                    text:"光强： 123h"
                    font.pointSize: 20
                    color:"#afafaf"
                }

                Text{
                    id:westText
                    y:30*Device.dentisty
                    text:"湿度 123p"
                    font.pointSize: 20
                    color:"#afafaf"
                }

            }
        }

        Rectangle{
            anchors.fill: parent
            color:"#01beb7"
            opacity: 0
            id:cap
            Text{
                anchors.centerIn: parent
                font.pointSize: 30
                text:"正在录音..."
                color:"#ffffff"
                NumberAnimation on scale{
                    from: 0.8
                    to:1.2
                    duration: 1000
                    loops: Animation.Infinite
                    running: true
                }
            }

            Behavior on opacity{
                PropertyAnimation{
                    properties: "opacity"
                    duration: 300
                }
            }
        }

        //bnts
        Row{
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 30*Device.dentisty
            spacing: (root.width-3*50*Device.dentisty)/4
            Image {
                id: camera_bnt
                source: "qrc:/imgs/home/smartHomeIcons/home/bnt_camera.png"
                width: 80*Device.dentisty
                height:width
                y:-width/2
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        stack.push({item:"qrc:/UI/Camera/CameraPage.qml"});
                    }
                }
            }
            Image {
                id: voice_bnt
                source: "qrc:/imgs/home/smartHomeIcons/home/bnt_voice.png"
                width: 80*Device.dentisty
                height:width
                Behavior on scale{
                    PropertyAnimation{
                        properties: "scale"
                        duration: 500
                        easing.type: Easing.OutBack
                    }
                }

                MouseArea{
                    anchors.fill: parent
                    onEntered: parent.scale=0.9;
                    onExited: parent.scale=1;
                    onPressAndHold: {
                        AudioRecorder.startRecord();
                        cap.opacity=1;
                    }
                    onReleased: {
                        cap.opacity=0;
                        AudioRecorder.stopRecord();
                        BaiduVoice.set_gettext();

                    }
                }
            }
            Image {
                id: control_bnt
                source: "qrc:/imgs/home/smartHomeIcons/home/bnt_control.png"
                width: 80*Device.dentisty
                height:width
                y:-width/2
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        stack.push({item:"qrc:/UI/Control/ControlPage.qml"});
                    }
                }
            }
        }

    }

//    Text{
//        anchors.centerIn: parent
//        font.pointSize: 25
//        color:"#12eeaa"
//        text:BaiduVoice.str
//        id:mtet
//    }

    property string voiceCommandText:BaiduVoice.str
    onVoiceCommandTextChanged: {
        var objs=new Array();
        var objs=getObjects(voiceCommandText);

        var cmd=getCommand(voiceCommandText);
        if(cmd==-1)
            return;

        for(var a=0;a<objs.length;a++){
//            console.log("********************************************",objs[a],",",cmd)
        }

        //reset timmer
        commandTimer.currentIndex=0;
        commandModel.clear();
//        ["关","开","暂停","高","提高","降低","开一档","开二档","开三档"]
        if(cmd==0){
            for(var a=0;a<objs.length;a++){
                var str=objs[a]+"_close#!";
                commandModel.append({"cmd":str});
                str=objs[a]+"_power_off#!";
                commandModel.append({"cmd":str});
            }
        }
        else if(cmd==1){
            for(var a=0;a<objs.length;a++){
                var str=objs[a]+"_open#!";
                commandModel.append({"cmd":str});
                str=objs[a]+"_power_on#!";
                commandModel.append({"cmd":str});
            }
        }
        else if(cmd==2){
            for(var a=0;a<objs.length;a++){
                var str=objs[a]+"_pause#!";
                commandModel.append({"cmd":str});
            }
        }
        else if(cmd==3){
            if(TcpSocket.tcpState==="connected")
                TcpSocket.sendData("air_01_temp_up#!");
            else
                Controler.send("air_01_temp_up");
        }
        else if(cmd==4){
            if(TcpSocket.tcpState==="connected")
                TcpSocket.sendData("air_01_temp_up#!");
            else
                Controler.send("air_01_temp_up");
        }
        else if(cmd==5){
            if(TcpSocket.tcpState==="connected")
                TcpSocket.sendData("air_01_temp_down#!");
            else
                Controler.send("air_01_temp_down");
        }
        else if(cmd==6){
            for(var a=0;a<objs.length;a++){
                var str=objs[a]+"_speed_01#!";
                commandModel.append({"cmd":str});
                console.log(str);
            }
        }
        else if(cmd==7){
            for(var a=0;a<objs.length;a++){
                var str=objs[a]+"_speed_02#!";
                commandModel.append({"cmd":str});
            }
        }
        else if(cmd==8){
            for(var a=0;a<objs.length;a++){
                var str=objs[a]+"_speed_03#!";/*#！#! #!*/
                commandModel.append({"cmd":str});
            }
        }

        commandTimer.restart();

    }

    function getObjects(str){
        var objTypes=["灯","吊扇","窗帘","空调"];
        var objtype=-1;
        var objects=new Array();
        for (var a=0;a<objTypes.length;a++){
            if(String(str).match(objTypes[a])==objTypes[a]){
                objtype=a;
//                mtet.text=BaiduVoice.str+objtype;
                break;
            }
        }
        if(objtype==0){
            if(String(str).match("全部")=="全部"||
                    String(str).match("所有")=="所有"){
                for(var a=1;a<=6;a++){
                    objects.push("light_0"+a);
                }
            }
            else if(String(str).match("一")=="一"){
                objects.push("light_01");
            }
            else if(String(str).match("二")=="二"){
                objects.push("light_02");
            }
            else if(String(str).match("三")=="三"){
                objects.push("light_03");
            }
            else if(String(str).match("四")=="四"||
                    String(str).match("最后")=="最后"){
                objects.push("light_04");
            }
        }
        else if(objtype==1){
            if(String(str).match("全部")=="全部"||
                    String(str).match("所有")=="所有"){
                for(var a=1;a<=4;a++){
                    objects.push("ceiling_fan_0"+a);
                }
            }
            else if(String(str).match("一")=="一"){
                objects.push("ceiling_fan_01");
            }
            else if(String(str).match("二")=="二"){
                objects.push("ceiling_fan_02");
            }
            else if(String(str).match("三")=="三"){
                objects.push("ceiling_fan_03");
            }
            else if(String(str).match("四")=="四"||
                    String(str).match("最后")=="最后"){
                objects.push("ceiling_fan_04");
            }
        }
        else if(objtype==2){
            if(String(str).match("左")=="左"){
                objects.push("curtain_03");
            }
            else if(String(str).match("右")=="右"){
                objects.push("curtain_01");
            }
            else if(String(str).match("全部")=="全部"||
                    String(str).match("所有")=="所有"){
                objects.push("curtain_01");
                objects.push("curtain_03");
            }

        }
        else if(objtype==3){
            objects.push("air_01");
        }
        return objects;
    }


    function getCommand(str){
        var commands=["关闭","打开","暂停","高","提高","降低","开一档","开二档","开三档"]
        var cmd=-1;
        for(var a=0;a<commands.length;a++){
            if(String(str).match(commands[a])==commands[a]){
                cmd=a;
                break;
            }
        }
        return cmd;
    }

}
